using System;
using System.Collections.Generic;
using System.Linq;
using MovieDB.Common;
using MovieDB.DTOs;
using MovieDB.RepositoryInterfaces;
using MovieDB.ServiceInterfaces;

namespace MovieDB.Services
{
    public class SearchService : ISearchService
    {
        private readonly IMovieRepository _movieRepository;

        public SearchService(IMovieRepository movieRepository)
        {
            _movieRepository = movieRepository;
        }

        public IEnumerable<MovieDto> SearchForAutocomplete(string movieName)
        {
            if (movieName == null)
            {
                throw new ArgumentNullException("movieName");
            }

            return _movieRepository.SearchForAutocomplete(movieName);
        }

        public PagedMovieDto SearchByMoviename(string id, int page, int pageSize)
        {
            if (id == null)
            {
                throw new ArgumentNullException("id");
            }

            var result = _movieRepository.SearchForAutocomplete(id);
            return _movieRepository.GetMoviesByIds(result.Select(movie => movie.Id).ToList(), page, pageSize);
        }

        public PagedMovieDto GetMovieByGenre(MovieGenre category, int page, int pageSize)
        {
            return _movieRepository.GetMovieByGenre(category, page, pageSize);
        }

        public PagedMovieDto GetMovieByVideoType(VideoType videoType, int page, int pageSize)
        {
            return _movieRepository.GetMovieByVideoType(videoType, page, pageSize);
        }

        public bool IsTitleAvailable(string title, Guid? movieId = null)
        {
            if (title == null)
            {
                throw new ArgumentNullException("title");
            }

            return _movieRepository.IsTitleAvailable(title, movieId);
        }
    }
}